
#include "notebook_widget.h"

/* FIXME: Use gettext! */
#define _(a) (a)

GtkWidget* create_tab_label_new (GtkNotebook*, GtkWidget*, const gchar*);

void on_close_tab_button_clicked (GtkButton*, gpointer);

GtkWidget*
create_notebook_widget ()
{

  GtkWidget *notebook;
  GtkWidget *child;

  notebook = gtk_notebook_new ();

  gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);


  /* FIXME: Test */
  child = gtk_label_new ("a");

  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child,\
    create_tab_label_new (GTK_NOTEBOOK (notebook), child, "title"));
  gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), child, TRUE);

  child = gtk_label_new ("b");

  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child,\
    create_tab_label_new (GTK_NOTEBOOK (notebook), child, "titlen"));
  gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), child, TRUE);

  return notebook;

}


GtkWidget*
create_tab_label_new (GtkNotebook *notebook, GtkWidget *child,\
  const gchar *str)
{

  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *image;
  GtkWidget *button;

  label = gtk_label_new (str);

  gtk_label_set_max_width_chars (GTK_LABEL (label), 20);
  gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
  gtk_label_set_single_line_mode (GTK_LABEL (label), TRUE);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_misc_set_padding (GTK_MISC (label), 0, 0);

  image = gtk_image_new_from_stock (GTK_STOCK_CLOSE,\
    GTK_ICON_SIZE_MENU);

  gtk_rc_parse_string ("style \"tab-close-button-style\"\n"\
          "{\n"\
          "GtkWidget::focus-padding = 0\n"\
          "GtkWidget::focus-line-width = 0\n"\
          "xthickness = 0\n"\
          "ythickness = 0\n"\
          "}\n"\
          "widget \"*.tab-close-button\" style \"tab-close-button-style\"");

  button = gtk_button_new ();

  gtk_button_set_image (GTK_BUTTON (button), image);
  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);

  gtk_widget_set_name (button, "tab-close-button");

  g_object_set_data (G_OBJECT (button), "child_widget", child);

  g_signal_connect (G_OBJECT (button), "clicked",\
    G_CALLBACK (on_close_tab_button_clicked), notebook);

  hbox = gtk_hbox_new (FALSE, 2);

  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

  gtk_widget_show_all (hbox);

  return hbox;

}


void
on_close_tab_button_clicked (GtkButton *button, gpointer user_data)
{

  gtk_notebook_remove_page (GTK_NOTEBOOK (user_data),\
    gtk_notebook_page_num (GTK_NOTEBOOK (user_data), GTK_WIDGET (\
      g_object_get_data (G_OBJECT (button), "child_widget"))));

}
